import { IFrame } from "./IFrame";
import { IBaseTimer } from "./IBaseTimer";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class Frame
    
    export  class Frame implements IFrame
    {
        constructor(arg1: number)
        {
            
            this._frameSortMap = new Map<any, any> /* flash.utils.Dictionary */(true);
            this._isPlay = false;
            this.delay = arg1;
            this._oneFrameMap = new Map<any, any> /* flash.utils.Dictionary */();
            return;
        }

        public dispatchEvent(): void
        {
            return;
        }

        public play(): void
        {
            this._isPlay = true;
            return;
        }

        public stop(): void
        {
            this._isPlay = false;
            return;
        }

        public addTimer(arg1: IBaseTimer): void
        {
            if (!this._isPlay) 
            {
                this.play();
            }
            if (arg1.delay != 1) 
            {
                this.pushFrameSort(arg1);
            }
            else 
            {
                this._oneFrameMap[arg1.id] = arg1;
            }
            var loc1;
            var loc2=((loc1 = this)._count + 1);
            loc1._count = loc2;
            return;
        }

        /* internal  */pushFrameSort(arg1: IBaseTimer): void
        {
            var loc1=this._frameCount + arg1.delay;
            var loc2=null;
            if (this._lastFrameCount != loc1) 
            {
                loc2 = this._frameSortMap[loc1] as Vector.<IBaseTimer>;
            }
            else 
            {
                loc2 = this._lastAry;
            }
            if (loc2) 
            {
                loc2[loc2.length] = arg1;
            }
            else 
            {
                loc2 = new Vector.<IBaseTimer>(0, false);
                loc2[0] = arg1;
                this._frameSortMap[loc1] = loc2;
            }
            this._lastFrameCount = loc1;
            this._lastAry = loc2;
            return;
        }

        protected onEnterFrameHandler(arg1: Object /* flash.events.Event */=null): void
        {
            var loc2=0;
            var loc1=flash.utils.getTimer();
            if (this._prevTime != 0) 
            {
                loc2 = loc1 - this._prevTime;
                this.renderer(loc2);
            }
            else 
            {
                loc2 = 0;
            }
            this._prevTime = loc1;
            return;
        }

        protected renderer(arg1: int): void
        {
            var loc2;
            var loc3=((loc2 = this)._frameCount + 1);
            loc2._frameCount = loc3;
            var loc1=this._frameSortMap[this._frameCount] as Vector.<IBaseTimer>;
            this.EnterFrameFor(loc1, arg1);
            return;
        }

        /* internal  */EnterFrameFor(arg1: Vector.<IBaseTimer>, arg2: int): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._oneFrameMap;
            for(loc1 of loc3) 
            {
                if (loc1.isDelete) 
                {
                    var loc4;
                    var loc5=((loc4 = this)._count - 1);
                    loc4._count = loc5;
                    delete this._oneFrameMap[loc1.id];
                    continue;
                }
                if (!loc1.running) 
                {
                    continue;
                }
                loc1.interval = arg2;
                loc1.renderer(this._frameCount);
            }
            if (arg1) 
            {
                loc2 = 0;
                loc3 = arg1;
                for(loc1 of loc3) 
                {
                    if (loc1.isDelete) 
                    {
                        loc5 = ((loc4 = this)._count - 1);
                        loc4._count = loc5;
                        loc1.dispose();
                        continue;
                    }
                    if (loc1.running) 
                    {
                        loc1.interval = arg2;
                        loc1.renderer(this._frameCount);
                    }
                    this.pushFrameSort(loc1);
                }
                arg1.length = 0;
                delete this._frameSortMap[this._frameCount];
            }
            return;
        }

        private /* var */_count: int;

        private /* var */_frameSortMap: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_frameCount: int=0;

        private /* var */_isPlay: Boolean;

        protected /* var */delay: number;

        private /* var */_lastFrameCount: int=-1;

        private /* var */_lastAry: Vector.<IBaseTimer>;

        private /* var */_oneFrameMap: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_prevTime: int=0;

        private /* var */_totalTime: int=0;
    }
